package com.elder.common.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ArraysUtils {
    /**
     * 将用 , 分隔的字符串转换成List<Long>
     * @author weiyx
     * @date 2020-10-15
     * @param character
     * @return java.util.List<java.lang.Long>
     */
    public static List<Long> strToLongList(String character){
        return strToLongList(character, ",");
    }

    /**
     * 将用分隔符分隔的字符串转换成List<Long>
     * @author weiyx
     * @date 2020-10-15
     * @param character
     * @return java.util.List<java.lang.Long>
     */
    public static List<Long> strToLongList(String character, String separator){
        if(StringUtils.isEmpty(character) || StringUtils.isEmpty(separator)) return new ArrayList<>();
        return Arrays.stream(character.split(separator)).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
    }

    /**
     * 将一个大list分隔成多个小list
     * @author weiyx
     * @date 2020-10-29
     * @param list  需要被分隔的list
     * @param eachSize  每个list多少条数据
     * @return java.util.List<java.util.List < T>>
     */
    public static <T> List<List<T>> separatedList(List<T> list, int eachSize){
        List<List<T>> result = new ArrayList<>();
        //计算余数
        int remainder = list.size() % eachSize;
        //计算需要多少个list
        int listNum = (list.size()  + eachSize - 1) / eachSize;
        for (int i = 0; i < listNum; i++) {
            List<T> eachList;
            Integer start = i * eachSize;
            Integer end = i * eachSize + eachSize;
            if(i == (listNum -1)) end = i * eachSize + remainder;
            eachList = list.subList(start, end);
            result.add(eachList);
        }
        return result;
    }
}
